package br.com.registrofinancas.database;

import java.util.ArrayList;
import java.util.List;

import br.com.registrofinancas.model.CategoryVO;
import br.com.registrofinancas.model.CategoryVO.CategoryType;
import br.com.registrofinancas.util.UsuarioUtils;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

public class CategoriasDAO {
	private Context contexto;
	private SQLiteDatabase db;
	private SQLiteStatement insertStatement;
	private static final String TABELA = "CATEGORIAS";
	private static final String INSERT = "INSERT INTO " + TABELA + "(id_usuario, tipo, descricao) VALUES (?,?,?)";
	
	public CategoriasDAO(Context contexto) {
		this.contexto = contexto;
		this.db = DataSource.geDBtInstance(this.contexto);
	}
	
	public long insert(CategoryVO categoria) {
		this.insertStatement = this.db.compileStatement(INSERT);		
		this.insertStatement.bindLong(1, categoria.getIdUser());
		this.insertStatement.bindLong(2, categoria.getType().ordinal());
		this.insertStatement.bindString(3, categoria.getName());
		return insertStatement.executeInsert();
	}
	
	public long delete( CategoryVO categoria ){
		String [] parametros = {categoria.getId().toString()};
		return db.delete(TABELA, "WHERE id = ?", parametros);
	}

	public long deleteAll( ) {
		return db.delete( TABELA, null, null ) ;
	}

	public List<CategoryVO> selectAll( ) {
		List<CategoryVO> list = new ArrayList<CategoryVO>( ) ;
		String[] parametros = {UsuarioUtils.getIdUsuario().toString()};
		Cursor cursor = 
				this.db.query( TABELA, new String[ ] { "id", "id_usuario", "tipo", "descricao" }, 
							   "id = ?", parametros, null, null, "id" ) ;
		if (cursor.moveToFirst( )) {
			do {
				CategoryVO categoria = new CategoryVO( ) ;
				categoria.setId( cursor.getInt( 0 ) ) ;
				categoria.setIdUser( cursor.getInt( 1 ) ) ;

				switch (cursor.getInt( 2 )) {
				case 0:
					categoria.setType( CategoryType.INCOME ) ;					
					break;
				case 1:
					categoria.setType( CategoryType.OUTCOME ) ;					
					break;					
				}

				categoria.setName( cursor.getString( 3 ) ) ;
				list.add( categoria ) ;
			}
			while (cursor.moveToNext( )) ;
		}
		if (null != cursor && !cursor.isClosed( )) {
			cursor.close( ) ;
		}
		return list ;
	}
	
	public List<CategoryVO> selectByTipo( CategoryType tipo ) {
		List<CategoryVO> list = new ArrayList<CategoryVO>( ) ;
		String[] parametros = {UsuarioUtils.getIdUsuario().toString() ,String.valueOf(tipo.ordinal())};
		Cursor cursor = 
				this.db.query( TABELA, new String[ ] { "id", "id_usuario", "tipo", "descricao" }, 
							   "id_usuario = ? AND tipo = ?", parametros, null, null, "id" ) ;
		if (cursor.moveToFirst( )) {
			do {
				CategoryVO categoria = new CategoryVO( ) ;
				categoria.setId( cursor.getInt( 0 ) ) ;
				categoria.setIdUser( cursor.getInt( 1 ) ) ;

				switch (cursor.getInt( 2 )) {
				case 0:
					categoria.setType( CategoryType.INCOME ) ;					
					break;
				case 1:
					categoria.setType( CategoryType.OUTCOME ) ;					
					break;					
				}
				
				categoria.setName( cursor.getString( 3 ) ) ;
				list.add( categoria ) ;
			}
			while (cursor.moveToNext( )) ;
		}
		if (null != cursor && !cursor.isClosed( )) {
			cursor.close( ) ;
		}
		return list ;
	}
	
	public CategoryVO selectById( Integer id ) {
		CategoryVO categoria = null ;
		String[] parametros = {String.valueOf(id)};
		Cursor cursor = 
				this.db.query( TABELA, new String[ ] { "id", "id_usuario", "tipo", "descricao" }, 
							   "id = ?", parametros, null, null, "id" ) ;
		if (cursor.moveToFirst( )) {
			categoria = new CategoryVO( ) ;
			categoria.setId( cursor.getInt( 0 ) ) ;
			categoria.setIdUser( cursor.getInt( 1 ) ) ;

			switch (cursor.getInt( 2 )) {
			case 0:
				categoria.setType( CategoryType.INCOME ) ;					
				break;
			case 1:
				categoria.setType( CategoryType.OUTCOME ) ;					
				break;					
			}
			
			categoria.setName( cursor.getString( 3 ) ) ;
		}
		if (null != cursor && !cursor.isClosed( )) {
			cursor.close( ) ;
		}
		return categoria;
	}	
	
}